<?php
class Api_Lib_Rsp_Employ {

    function __construct() {
        $this->_modelExample = FLEA::getSingleton('Model_Jichu_Employ');
        $this->_modelDep = FLEA::getSingleton('Model_Jichu_Department');
    }

    /**
     *
     * Time：2019/11/07 14:01:47
     * @author li
     * @param 参数类型
     * @return 返回值类型
    */
    function update($params,& $service){
        if(!$params['linkId']){
            $service->send_user_error('linkId required');
        }

        $params['employCode'] = strval($params['employCode']);
        //检查是否存在
        $employ = $this->_modelExample->find(array('linkId'=>$params['linkId']));
        $dep = $this->_modelDep->find(array('depName'=>$params['depName']));
        if(!$dep){
            $data = array('depName'=>$params['depName']);
            $depId = $this->_modelDep->save($data);
        }

        $_data = array(
            'id'         => $employ['id']+0,
            'employName' => $params['employName'],
            'employCode' => $params['employCode'],
            'depId'      => $dep ? $dep['id'] : intval($depId),
            'email'      => $params['email'],
            'mobile'     => $params['mobile'],
            'linkId'     => $params['linkId'],
        );

        //数据保存
        $result = $this->_modelExample->save($_data);
        $employ['id'] = $employ['id'] ? $employ['id'] : $result;

        return array('success'=>$result ? true :false ,'msg'=>'完成','data'=>array('id'=>$employ['id']));
    }

    /**
     *
     * Time：2019/11/07 14:01:47
     * @author li
     * @param 参数类型
     * @return 返回值类型
    */
    function delete($params,& $service){
        if(!$params['linkId']){
            $service->send_user_error('linkId required');
        }

        $_modelChuku= FLEA::getSingleton('Model_Sample_Chuku');
        $params['linkId'] = strval($params['linkId']);

        //查找是否被使用，如果被使用则禁止删除
        $employ = $this->_modelExample->find(array('linkId'=>$params['linkId']));
        //查找是否有用到
        $count = $_modelChuku->findCount(array('employId'=>$employ['id']));

        if($count > 0){
            $res = false;
        }else{
            $res = $this->_modelExample->removeByConditions(array('linkId'=>$params['linkId']));
        }

        return array('success'=>$res ,'msg'=>'完成');
    }


    /**
     * 获取客户列表
     * Time：2019/11/07 15:09:10
     * @author li
     * @param 参数类型
     * @return 返回值类型
    */
    function getList($params,& $service){
        $pageNum     = $params['pageNum'] ? $params['pageNum'] : 20;
        $currentPage = $params['page'] ? $params['page'] : 1;

        $condition = array();
        if($params['employName']!='') {
            $condition[] = array('employName',"%{$params['employName']}%",'like');
        }
        if($params['employCode']!='') {
            $condition[] = array('employCode',"%{$params['employCode']}%",'like');
        }

        // $this->_modelExample->clearLinks();
        FLEA::loadClass('TMIS_Pager');
        $pager = & new TMIS_Pager($this->_modelExample,$condition,'id desc',$pageNum ,($currentPage - 1));
        $rowset = $pager->findAll();
        //把需要的数据带出来
        $rows = array();
        foreach ($rowset as $key => & $v) {

            $rows[] = array(
                'id'         => $v['id'],
                'employName' => $v['employName'],
                'employCode' => $v['employCode'],
                'email'      => $v['email'],
                'mobile'     => $v['mobile'],
                'depId'      => $v['depId'],
                'isFire'     => $v['isFire'],
                'fireText'   => $v['isFire'] == 0 ? '正常' : '已离职',
                'depName'    => $v['Dep']['depName'],
            );
        }

        return array('row'=>$rows,'page'=>$currentPage,'total'=>$pager->totalCount,'pageCount'=>count($rows));
    }
}
?>